package com.example.project;

import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.HashMap;
import com.google.android.maps.GeoPoint;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;


public class LazyAdapter extends BaseAdapter implements Filterable{
    private Activity activity;
    private ArrayList<HashMap<String, String>> data;
    private static LayoutInflater inflater=null;
    public ImageLoader imageLoader; 
    GPSTracker gps;
    private static final NumberFormat nf = new DecimalFormat("##.####");    
    ArrayList<HashMap<String, String>> mOriginalValues; 
    
    public LazyAdapter(Activity a, ArrayList<HashMap<String, String>> d) {
        activity = a;
        data=d;
        mOriginalValues=d;
        inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        imageLoader=new ImageLoader(activity.getApplicationContext());
    }

    public int getCount() {
        return data.size();
    }
    
    public GeoPoint getPoint(int position){
    	GeoPoint point1 = new GeoPoint((int)(Float.parseFloat(data.get(position).get(Menjar.KEY_LATITUD))* 1E6), (int) (Float.parseFloat(data.get(position).get(Menjar.KEY_LONGITUD))* 1E6));
    	return point1;
    }

    public Object getItem(int position) {
    	GeoPoint point1 = new GeoPoint((int)(Float.parseFloat(data.get(position).get(Menjar.KEY_LATITUD))), (int) (Float.parseFloat(data.get(position).get(Menjar.KEY_LONGITUD))));
    	return point1;
    }

    public long getItemId(int position) {
        return position;
    }
    
    public View getView(int position, View convertView, ViewGroup parent) {
        View vi=convertView;
        if(convertView==null)
            vi = inflater.inflate(R.layout.list_row, null);

        TextView title = (TextView)vi.findViewById(R.id.title); // title
        TextView artist = (TextView)vi.findViewById(R.id.artist); // artist name
        RatingBar qual = (RatingBar)vi.findViewById(R.id.ratingBar1);
        TextView dist = (TextView)vi.findViewById(R.id.dist); // duration
        ImageView thumb_image=(ImageView)vi.findViewById(R.id.list_image); // thumb image
        
        HashMap<String, String> song = new HashMap<String, String>();
        song = data.get(position);
        gps = new GPSTracker(parent.getContext());
        // Setting all values in listview
       title.setText(song.get(Menjar.KEY_NAME));
        artist.setText(song.get(Menjar.KEY_DIRECTION));
        if(song.get(Menjar.KEY_VALORACIOMITJA) == null){
        	qual.setRating((float) 3.0);
        }else{
        qual.setRating(Float.parseFloat(song.get(Menjar.KEY_VALORACIOMITJA)));
        }
        if (!gps.canGetLocation()){
        	dist.setText("no dist gps off");
        }else{
        dist.setText(song.get(Menjar.KEY_DIST)+" m");
        
        }
      imageLoader.DisplayImage(song.get(Menjar.KEY_THUMB_URL), thumb_image);
        return vi;
    }

	public Filter getFilter() {
		 Filter filter = new Filter() {

             @SuppressWarnings("unchecked")
             @Override
             protected void publishResults(CharSequence constraint,FilterResults results) {

            	 data = (ArrayList<HashMap<String, String>>) results.values; // has the filtered values
                 notifyDataSetChanged();  // notifies the data with new filtered values
             }

             @Override
             protected FilterResults performFiltering(CharSequence constraint) {
                 FilterResults results = new FilterResults();        // Holds the results of a filtering operation in values
                 ArrayList<HashMap<String, String>> FilteredArrList = new ArrayList<HashMap<String, String>>();

                 if (mOriginalValues == null) {
                     mOriginalValues = new ArrayList<HashMap<String, String>>(); // saves the original data in mOriginalValues
                 }

                 /********
                  * 
                  *  If constraint(CharSequence that is received) is null returns the mOriginalValues(Original) values
                  *  else does the Filtering and returns FilteredArrList(Filtered)  
                  *
                  ********/
                 if (constraint == null || constraint.length() == 0) {

                     // set the Original result to return  
                     results.count = mOriginalValues.size();
                     results.values = mOriginalValues;
                 } else {
                     
                     for (int i = 0; i < mOriginalValues.size(); i++) {
                         HashMap<String, String> d = mOriginalValues.get(i);
                         if (d.get(Menjar.KEY_NAME).startsWith(constraint.toString())) {
                             FilteredArrList.add(d);
                         }
                     }
                     // set the Filtered result to return
                     results.count = FilteredArrList.size();
                     results.values = FilteredArrList;
                 }
                 return results;
             }
         };
         return filter;
	}

}
